//
//  LYPersonalListCell.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/31.
//

import UIKit

class LYPersonalListCell: UITableViewCell {
    
    var didClickButtonClourse: ((Megaloplanesque) -> Void)?
    var megaloplanesque: Megaloplanesque? = nil
    var didTextFieldChanged: ((Megaloplanesque) -> Void)?
    
    static let leftRMargin = 16.0
    static let top = 15.0
    private lazy var backgroundImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = false
        iv.image = UIImage(named: "personal_list_bg")
        iv.isUserInteractionEnabled = true
        return iv
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 18)
        label.textAlignment = .center
        label.numberOfLines = 0
        label.textColor = UIColor(hex: "#000000")
        label.textAlignment = .left
        label.text = "saappppss"
        return label
    }()
    
    private lazy var textField: LYTextField = {
        let textField = LYTextField(backgroundImage: nil)
        return textField
    }()
    
    lazy var arrowButton: UIButton = {
        let button = UIButton(type: .custom)
        button.addTarget(self, action: #selector(arrowButtonAction(_:)), for: .touchUpInside)
        return button
    }()
    
    private lazy var arrowImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = false
        iv.image = UIImage(named: "arrow_alert")
        return iv
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        
        commonInitSubviews()
    }
    
    func commonInitSubviews() {
        contentView.addSubview(backgroundImageView)
        backgroundImageView.addSubview(titleLabel)
        textField.textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        backgroundImageView.addSubview(textField)
        backgroundImageView.addSubview(arrowButton)
        backgroundImageView.addSubview(arrowImageView)
        setLayoutSubviews()
    }
    
    func setLayoutSubviews() {
        backgroundImageView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(LYPersonalListCell.top)
            make.bottom.equalToSuperview()
            make.leading.trailing.equalToSuperview().inset(LYPersonalListCell.leftRMargin)
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(14)
            make.leading.trailing.equalToSuperview().inset(10)
        }
        
        textField.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(40)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        arrowButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(40)
            make.leading.trailing.equalTo(titleLabel)
            make.bottom.equalToSuperview()
        }
        
        arrowImageView.snp.makeConstraints { make in
            make.centerY.equalTo(textField)
            make.trailing.equalToSuperview().offset(-16)
        }

    }
    
    func configure(with megaloplanesque: Megaloplanesque) {
        self.megaloplanesque = megaloplanesque
        if let shotish = megaloplanesque.shotish {
            if let cellType = PersonalListCellType(rawValue: shotish) {
                switch cellType {
                case .enum, .citySelect:
                    arrowButton.isHidden = false
                    arrowImageView.isHidden = false
                case .txt:
                    arrowButton.isHidden = true
                    arrowImageView.isHidden = true
                    // "flectacle":0, //【重要】是否数字键盘：0否，1是（系统原生数字键盘）
                    if let flectacle = megaloplanesque.flectacle,
                       flectacle == 1 {
                        self.textField.keyboardType = .numberPad
                    } else {
                        self.textField.keyboardType = .default
                    }
                }
            }
            
            if let cipitfication = megaloplanesque.cipitfication {
                self.titleLabel.text = cipitfication
            }
            
            if let call = megaloplanesque.call {
                self.textField.textField.placeholder = call
            }
            
            if let familiose = megaloplanesque.familiose {
                self.textField.textField.text = familiose
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func arrowButtonAction(_ sender: UIButton) {
       
        guard let megaloplanesque = megaloplanesque else { return }
        didClickButtonClourse?(megaloplanesque)
    }
    
    @objc func textFieldDidChange(_ textField: UITextField) {
        self.megaloplanesque?.familiose = textField.text
        guard let megaloplanesque = self.megaloplanesque else { return }
        self.didTextFieldChanged?(megaloplanesque)
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
}
